﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NSCore
{

    /// <summary>
    /// 提供基础的声级转换算法
    /// </summary>
    public static class NSMathHelper
    {


        public static double ADDNoiseLevel(double nl1, double nl2)
        {

            //            噪声值是声压的对数乘以10
            //设某处两个需要叠加的噪声值相同为N，声压为P
            //N = 10 lg(P)
            //因此噪声值不能直接相加，需要反算成声压再相加。
            //P = 10^(N/10)
            //叠加
            //2P = 2×10^(N/10)
            //取对数得叠加后的噪声值
            //N2 = N + 10lg2
            //     = N + 3





            //首先得到声压

            double pressure1 = NSMathHelper.GetNoisePressure(nl1);



            double pressure2 = NSMathHelper.GetNoisePressure(nl2);


            double pressure = pressure1 + pressure2;




            //然后计算当前声压

            return NSMathHelper.GetNoiseLevel(pressure);



 

        }


        public static double GetNoisePressure(double nl)
        {

            return Math.Pow(10, (nl / 10.0d));
 

        }

        public static double GetNoiseLevel(double pressure)
        {
            return  Math.Log10(pressure)*10;
 
        }

    }
}
